from .serializer import *
from .models import *
from common.Pagenate import LargeResultsSetPagination
from rest_framework import viewsets, mixins, permissions,generics,status
from django_filters.rest_framework import DjangoFilterBackend
from django.http import HttpResponse,JsonResponse
from django.views.generic.base import View
# Create your views here.
from django.contrib.auth.models import Group,User
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

class HostListView(generics.ListCreateAPIView):
    queryset = Host.objects.all()
    serializer_class = HostSerializer
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        user_groups = Group.objects.filter(user__username=request.user).all()
        user_indices = Host.objects.filter(groups__in=user_groups)
        serializer = self.get_serializer(user_indices, many=True)
        return Response(serializer.data)

class HostDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Host.objects.all()
    serializer_class = HostSerializer
    permission_classes = [IsAuthenticated]

class GroupListView(generics.ListCreateAPIView):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    filter_backends = [DjangoFilterBackend]
    paginator = LargeResultsSetPagination()

class GroupDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer